import java.util.Arrays;

public class Homework {
    //模拟toString方法
    public static String toString(int[] array){
        String str = "[";
        for(int i =0;i<array.length;i++){
            if(i<array.length-1){
                str += array[i]+",";
            }else{
                str += array[i]+"]";
            }
        }
        return str;
    }
    public static void main1(String[] args) {
        int[] array = new int[]{1,2,3};
        String result = toString(array);
        System.out.println(result);
    }
    //模拟copyOf方法
    public static int[] copyOf(int[] arr,int length){
        int[] arr2 = new int[length];
        for(int i=0;i<length;i++){
            arr2[i] = arr[i];
        }
        return arr2;
    }

    public static void main2(String[] args) {
        int[] arr1 = new int[]{1,2,3,4};
        int len = arr1.length;
        int[] newarr;
        newarr = copyOf(arr1,len);
        System.out.println("newarr:"+toString(newarr));
    }
    //二分查找binarySearch
    public static int binarySearch(int[] arr,int toFind){
        int left = 0;
        int right = arr.length-1;
        while(left<=right){
            int mid = (left+right)/2;
            if(toFind<arr[mid]){
                right = mid-1;
            }else if(toFind>arr[mid]){
                left = mid+1;
            }else{
                return mid;
            }
        }
        return -1;
    }

    public static void main3(String[] args) {
        int[] array=new int[]{1,2,3,4,5};
        System.out.println(binarySearch(array,4));
    }
    //判断数组是否乱序(递增)
    public static int seq(int[] arr){
        for(int i=0;i<arr.length;i++){
            for(int j=i+1;j<arr.length;j++){
                if(arr[i]>arr[j]) {
                    return -1;
                }
            }
        }
        return 1;
    }

    public static void main4(String[] args) {
        int[] array = new int[]{1,2,4,3,5};
        System.out.println(seq(array));
    }
    //冒泡排序
    public static void sort(int[] arr){
        int temp = 0;
        for(int i = 0;i<arr.length;i++){
            int count = 0;
            for(int j = 1;j<arr.length-i;j++)
                if(arr[j-1]>arr[j]){
                    temp = arr[j-1];
                    arr[j-1] = arr[j];
                    arr[j] = temp;
                    count++;
                }
            if(count==0){
                break;
            }
        }
    }

    public static void main(String[] args) {
        int[] array = new int[]{6,2,5,4,1};
        System.out.println(Arrays.toString(array));
        sort(array);
        System.out.println(Arrays.toString(array));
    }
}
